﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using Com.Toook.AutoDownloadHtml.DownloadHtml.ParsePage;
using Com.Toook.AutoDownloadHtml.DownloadHtml.ParsePage.Entity;
using NSoup.Select;
using Com.Toook.Common;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //ParseXML();        
    }
    protected void ButtonReflect_Click(object sender, EventArgs e)
    {
        //CreateKindByXml createKind = new CreateKindByXml(Server.MapPath("~\\testXD.xml"));
        //Kind kind = createKind.GetKind("xdDZ");
        //string s = kind.BaseUrl;
        string xmlFilePath = Server.MapPath("~\\testXD.xml");
        GetKindResultFromHtml html = new GetKindResultFromHtml(xmlFilePath, "xdDZ");
        html.GetKindPageResults();
    }
    private void ParseXML()
    {
        XmlDocument doc = new XmlDocument();
        string path = Server.MapPath(@"~\App_Data\Nodes.xml");
        doc.Load(path);

        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.ChildNodes;
        foreach (XmlNode node in nodes)
        {
            ParseXmlNode(node);
        }
        showText.Text = sbResult.ToString();
    }
    private void ParseXmlNode(XmlNode node)
    {
        if (!node.HasChildNodes)
        {
            sbResult.Append("Leaf=" + node.InnerText + "<br/>");
            //ShowElement();
        }
        else
        {
            if (node.HasChildNodes && node.Name == "node")
            {
                sbResult.Append("return=" + node.Attributes["return"].Value + ", GetBy=" + node.Attributes["getBy"].Value + "<br/>");
                NSoup.Select.Elements elements = GetElements("", "titleClass", new NSoup.Nodes.Document(""));
                if (elements == null || elements.Count < 1)
                {
                    return;
                }
                else
                {
                    //this.currentElements = elements;
                }
            }

            XmlNodeList nodes = node.ChildNodes;
            NSoup.Select.Elements es = new NSoup.Select.Elements();
            foreach (XmlNode n in nodes)
            {
                ParseXmlNode(n);  
              
                ////////
                if (n.Attributes["return"].Value == "ES")
                {
                    es = doc.GetElementsByAttribute("");
                }
                else
                {
                    es = new NSoup.Select.Elements(1);
                    es[0] = doc.GetElementById("");
                }

                if (n.Name == "leaf")
                {
                    resultElements = es[0].GetElementsByClass(""); //elements[0].Select("");//
                }
            }
        }
    }
    private NSoup.Select.Elements resultElements;
    private NSoup.Nodes.Document doc;
    private StringBuilder sbResult = new StringBuilder();
    private void ShowElement()
    {
        sbResult.Append("        show Element <br/>");
    }
    private void ShowElements(XmlNode node)
    {
        if (node.Attributes["return"].Value == "Elements")
        {
            //Elements elements = doc.GetElementsByClass("");
            //GetElements(type, name, doc);
        }
        else
        {

        }
        //sbResult.Append("        show Elementssss <br/>");
    }
    private NSoup.Select.Elements GetElements(string type, string name, NSoup.Nodes.Document doc)
    {
        NSoup.Select.Elements elements = null;
        switch (type)
        {
            case "class":
                elements = doc.GetElementsByClass(name);
                break;
            case "tag":
                elements = doc.GetElementsByTag(name);
                elements[0].GetElementById("");
                break;

            default:
                break;
        }
        return elements;
    }

    private NSoup.Select.Elements getLinks(NSoup.Select.Elements elements, XmlNodeList steps, object param)
    {
        NSoup.Select.Elements results = new NSoup.Select.Elements();
        foreach (XmlNode step in steps)
        {
            
            foreach (NSoup.Nodes.Element e in elements)
            {
                NSoup.Select.Elements es = e.GetElementsByClass("list14");
                foreach (NSoup.Nodes.Element r in es)
                {
                    results.Add(r);
                }
            }
        }

        return results;
    }

    //private void ParseXML()
    //{
    //    XmlDocument doc = new XmlDocument();
    //    string path = Server.MapPath(@"~\App_Data\Nodes.xml");
    //    doc.Load(path);

    //    XmlElement root = doc.DocumentElement;
    //    XmlNodeList nodes = root.ChildNodes;
    //    NSoup.Select.Elements resultElements = new NSoup.Select.Elements();//doc.getElementByTag("");
    //    foreach (XmlNode node in nodes)
    //    {
    //        //SearchParam = buildParam(node);
    //        resultElements = getLinks(resultElements, new object());
    //    }
    //    showText.Text = sbResult.ToString();
    //}

    private void GetNews()
    {
        string url = "http://news.sohu.com/guoneixinwen.shtml";
        NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect(url).Get();
        StringBuilder str = new StringBuilder();
        StringDictionary sd = new StringDictionary();

        NSoup.Select.Elements elements = doc.GetElementsByClass("column370");
        NSoup.Select.Elements divs = elements[0].GetElementsByClass("f14list");
        foreach (NSoup.Nodes.Element div in divs)
        {
            NSoup.Select.Elements links = div.GetElementsByTag("a");
            foreach (NSoup.Nodes.Element link in links)
            {
                if (link.Attr("href").StartsWith("http://news.sohu"))
                {
                    str.Append("link=" + link.Attr("href"));

                    str.Append(", text=" + link.Text()).Append("<br />");
                    sd.Add(link.Attr("abs:href"), link.Text());
                }
            }
        }
        //国内 http://news.sina.com.cn/c/2013-04-01/
        //title id = artibodyTitle   content id = artibody
        foreach (DictionaryEntry d in sd)
        {
            doc = NSoup.NSoupClient.Connect(d.Key.ToString()).Get();

            string txt = doc.Html();
            string title = doc.GetElementsByTag("h1").Html();

            string c = doc.GetElementById("contentText").Html();
            NSoup.Nodes.Element cc = doc.GetElementById("contentText");
            cc.GetElementsByTag("");

            str.Append("<br /><br />").Append(title).Append("<br />").Append(c).Append("<br /><br />");
        }

        showText.Text = str.ToString();
    }
    protected void ButtonTaobaoImg_Click(object sender, EventArgs e)
    {
        string url = "http://guang.taobao.com/square/index.htm?cat_id=15&tag_id=0&sort=0&type=0&cpage=2";
        NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect(url).Get();
        Elements elements = doc.GetElementsByTag("img");
        for (int i = 0; i < 3 && i < elements.Count; i++)
        {
            string img = elements[i].Attr("src");
        }
    }
    protected void ButtonTransfer_Click(object sender, EventArgs e)
    {
        //CreateContentByXml createContent = new CreateContentByXml(Server.MapPath("~\\testXD.xml"));
        //DetailRoadMap roadMap = createContent.GetDetailRoadMap("xdDZ");
        string siteKindId = TextBoxSiteKindId.Text;
        string path = Server.MapPath("~\\App_Data\\Joke\\www.jokeji.cn.xml");//haha365.com.xml
        
        GetKindResultFromHtml html = new GetKindResultFromHtml(path, siteKindId);            
        html.GetKindPageResults();       
    }

    private static string[] jokeIds = {
    "jokeji_小笑话",    
    "jokeji_爆笑男女",
    "jokeji_社会",
    "jokeji_冷笑话",
    "jokeji_校园",
    "jokeji_综合",
    "jokeji_儿童",
    "jokeji_夫妻",
    "jokeji_动物",
    "jokeji_家庭",
    "jokeji_职场",
    "jokeji_短信",
    "jokeji_爆笑签名",
    "jokeji_幽默网文",
    "jokeji_幽默笑话",
    "jokeji_影视台词",
    "jokeji_爱情",
    "jokeji_文艺",
    "jokeji_医疗",
    "jokeji_名著暴笑",
    "jokeji_愚人",
    "jokeji_民间",
    "jokeji_交通",
    "jokeji_哲理",
    "jokeji_节日",
    "jokeji_名人",
    "jokeji_国际",
    "jokeji_体育",
    "jokeji_网络",
    "jokeji_宗教",
    "jokeji_电脑",
    "jokeji_恋爱",
    "jokeji_司法",
    "jokeji_恐怖",
    "jokeji_搞笑测试",
    "jokeji_军事",
    "jokeji_搞笑歌词",
    "jokeji_顺口溜",
    "jokeji_感情",
    "jokeji_女人",
    "jokeji_股市证券",
    "jokeji_传说",
    "jokeji_爆笑笑话",
    "jokeji_求爱秘籍",
    "jokeji_英语"
     }   ;
    protected void ButtonTransferAllJokeJi_Click(object sender, EventArgs e)
    {
        string siteKindId = TextBoxSiteKindId.Text;
        string path = Server.MapPath("~\\App_Data\\Joke\\www.jokeji.cn.xml");
        foreach (string siteId in jokeIds)
        {
            //GetKindResultFromHtml html = new GetKindResultFromHtml(path, siteKindId);
            GetKindResultFromHtml html = new GetKindResultFromHtml(path, siteId);
            html.GetKindPageResults();
        }
    }
    //public static readonly ILog log = LogManager.GetLogger("default.aspx");
    protected void ButtonScript_Click(object sender, EventArgs e)
    {        

        string xmlFilePath = Server.MapPath("~\\App_Data\\Movie\\xddy.cc.xml");
        //GetKindResultFromHtml html = new GetKindResultFromHtml(xmlFilePath, "xddy_xj");
        //html.GetKindPageResults();
        string siteId = TextBoxSiteKindId.Text;
        CreateContentByXml createContent = new CreateContentByXml(xmlFilePath);
        DetailRoadMap roadMap = createContent.GetDetailRoadMap(siteId);
        GetContentResultFromHtml html = new GetContentResultFromHtml(roadMap);
        html.GetContent("");

        //foreach(string siteId in xddyId)
        //{
        //    GetKindResultFromHtml html = new GetKindResultFromHtml(xmlFilePath, siteId);
        //    html.GetKindPageResults();
        //}
      /*  
        string url = "http://www.xddy.cc/play/52441.html";
        NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect(url).Get();
        StringBuilder str = new StringBuilder();

        NSoup.Nodes.Element element = doc.GetElementById("player");
        NSoup.Select.Elements script = element.GetElementsByTag("script").Eq(0);
        script = element.GetElementsByIndexEquals(0);
        string t = script[0].Data;
        */ 
    }
    private static string[] xddyId = {
"xddy_dz",
"xddy_xj",
"xddy_kb",
"xddy_aq",
"xddy_gs",
"xddy_zz",
"xddy_kh",
"xddy_anime"
};
    protected void Button2_Click(object sender, EventArgs e)
    {
        string a = ":";
        string b = a;
    }
}

